package gradingcalculator;

public class Subject {
    
    //Attributes
    private String name;
    private int units;
    private Item[] items;
    
    //Constructor
    public Subject(String name, int units) {
        this.name = name;
        this.units = units;
        this.items = new Item[4];
        
        items[0] = new Item("Homework",10);
        items[1] = new Item("Quiz",20);
        items[2] = new Item("Test",30);
        items[3] = new Item("Exam",40);
    }
    //Constructor
    public Subject(String name) {
        this.name = name;
        this.units = 3;
        this.items = new Item[4];
        
        items[0] = new Item("Homework",10);
        items[1] = new Item("Quiz",20);
        items[2] = new Item("Test",30);
        items[3] = new Item("Exam",40);
    }
    
    //Returns
    public String getName(){
        return name;
    }
    public int getUnits(){
        return units;
    }
    public Item[] getItems(){
        return items;
    }
    public Item getItem(int index){
        return items[index];
    }
    
    //Modifiers
    public void setName(String name){
        this.name = name;
    }
    public void setUnits(int units){
        this.units = units;
    }
    public void setItems(Item[] items){
        this.items = items;
    }
    public void setItem(Item item, int index){
        this.items[index] = item;
    }
    
// ===== Advanced Modifiers (Adds, Counters, and Array Manipulators)============
    
    //ADDS
    public void addItem(Item item){
        int length = this.items.length;
        Item[] proxy = new Item[length+1];
        System.arraycopy(this.items, 0, proxy, 0, length);
        proxy[length] = item;
        this.items = proxy;
    }
    public void removeItem(Item item, int index){
        int length = this.items.length;
        Item[] proxy = new Item[length-1];
        System.arraycopy(this.items, 0, proxy, 0, index);
        System.arraycopy(this.items, index, proxy, index, length-index);
        this.items = proxy;
    }
    public void addItem(String name, double weight){
        //Create an element in the array if the array is null
        if(items == null) { items = new Item[1]; }
        //Otherwise add another element to the end of array
        else { expandItemsBy(1); }
        //Create a Item in the Grading Calculator named 'name parameter'
        items[items.length-1] = new Item(name, weight);
    }
    //ARRAY MANIPULATOR
    private void expandItemsBy(int i) {
        Item[] proxy = new Item[items.length+i];
        System.arraycopy(items, 0, proxy, 0, items.length);
        items = proxy;
    }
    
}
